package com.jy.rhin.facade.infra.controller.node;

import com.jy.framework.base.entity.Result;
import com.jy.rhin.domain.document.node.NodeAuthenticationService;
import com.jy.rhin.infra.repo.node.query.NodeAuthenticationQueryHandler;
import com.jy.rhin.sdk.dto.node.NodeAuthenticationBaseDto;
import com.jy.rhin.sdk.dto.node.NodeAuthenticationDto;
import com.jy.rhin.sdk.dto.node.query.NodeAuthenticationReqDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 */
@RestController
@RequestMapping("/infra/node/authentication")
@Api(tags = "节点认证api")
@Slf4j
public class NodeAuthenticationController {

    @Autowired
    private NodeAuthenticationService nodeAuthenticationService;
    @Autowired
    private NodeAuthenticationQueryHandler nodeAuthenticationQueryHandler;


    @ApiOperation("查询节分页列表")
    @GetMapping("/getNodePage")
    public Result<com.jy.framework.base.entity.Page<NodeAuthenticationDto>> getNodePage(NodeAuthenticationReqDto dto) {
        return Result.success(nodeAuthenticationQueryHandler.getNodePage(dto));
    }


    @ApiOperation(value = "新增修改节点信息", notes = "新增修改节点信息")
    @PostMapping("/addOrUpdateNode")
    public Result addOrUpdateNode(@RequestBody NodeAuthenticationDto nodeAuthenticationDto) throws Exception {
        nodeAuthenticationService.addOrUpdateNode(nodeAuthenticationDto);
        return  Result.success();
    }


    @ApiOperation(value = "审核节点认证", notes = "审核节点认证")
    @PostMapping("/auditNode")
    public Result auditNode(@RequestBody NodeAuthenticationDto nodeAuthenticationDto) throws Exception {
        nodeAuthenticationService.auditNode(nodeAuthenticationDto);
        return  Result.success();
    }

    @ApiOperation(value = "节点认证详情", notes = "节点认证详情")
    @ApiImplicitParam(name = "id", value = "id")
    @GetMapping("/getNodeDetail")
    public Result<NodeAuthenticationDto> getNodeDetail( @RequestParam Long id) {
        return  Result.success(nodeAuthenticationQueryHandler.getNodeDetail(id));
    }

    @ApiOperation(value = "获取节点列表", notes = "获取节点列表")
    @ApiImplicitParam(name = "nodeName", value = "nodeName")
    @GetMapping("/getNodeList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "nodeName", value = "文档id", required = false)
            })
    public Result<List<NodeAuthenticationDto>> getNodeList(@RequestParam(value="nodeName",required = false) String nodeName) {
        return  Result.success(nodeAuthenticationQueryHandler.getNodeList(nodeName));
    }

    @ApiOperation(value = "通过公钥获取节点信息", notes = "获取节点信息")
    @GetMapping("/getNodeByGY")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "nodeName", value = "文档id", required = false)
    })
    public Result<NodeAuthenticationBaseDto> getNodeByGY(@RequestParam("publicKey")String publicKey, @RequestParam("action")String action) {
        return  Result.success(nodeAuthenticationQueryHandler.getNodeByGY(publicKey,action));
    }

}
